﻿using System;
using Gfw.Infrastructure.ExceptionImpl;
using Gfw.Infrastructure.Facilities;
using Gfw.Models.Stock;
using Gfw.Repositories.Stock;

namespace Gfw.Infrastructure.Stock.Facilities
{
    public class TradeFacility : IFacility
    {
        private StkBargainagency _agency;

        public TradeFacility(StkBargainagency agency)
        {
            _agency = agency;
            _agency.Id = Guid.NewGuid().ToString();
        }

        public void Execute(Repository.UnitOfWork unitOfWork = null)
        {
            if (unitOfWork == null)
                throw new GfwTransactionInvalidException("交易类型未关联事务，这个操作是无意义的。");
            WaitBargainPoolRepository poolRep = new WaitBargainPoolRepository();
            var pool = new StkWaitbargainpool()
                           {
                               Id = Guid.NewGuid().ToString(),
                               AgencyCount = _agency.AgencyCount,
                               AgencyDate = _agency.AgencyDate,
                               AgencyId = _agency.Id,
                               AgencyPrice = _agency.AgencyPrice,
                               TradeType = _agency.TradeType,
                               UserId = _agency.Userid,
                               AccountType = _agency.AccountType
                           };
            BargainAgencyRepository agencyRep = new BargainAgencyRepository();
            agencyRep.Save(_agency, unitOfWork);
            poolRep.Save(pool, unitOfWork);

        }
    }
}
